/**
 * 
 */
package org.swing.utility.jdbc.helper;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author lqnhu
 *
 */
public class QueryLoader {
	/**
	 * The Singleton instance of this class.
	 */
	private static final QueryLoader instance = new QueryLoader();

	/**
	 * Return an instance of this class.
	 * 
	 * @return The Singleton instance.
	 */
	public static QueryLoader instance() {
		return instance;
	}

	/**
	 * Maps query set names to Maps of their queries.
	 */
	private final Map<String, Map<String, String>> queries = new HashMap<String, Map<String, String>>();

	/**
	 * QueryLoader constructor.
	 */
	protected QueryLoader() {
		super();
	}

	/**
	 * Loads a Map of query names to SQL values. The Maps are cached so a
	 * subsequent request to load queries from the same path will return the
	 * cached Map.
	 *
	 * @param path
	 *            The path that the ClassLoader will use to find the file. This
	 *            is <strong>not</strong> a file system path. If you had a
	 *            jarred Queries.properties file in the com.yourcorp.app.jdbc
	 *            package you would pass
	 *            "/com/yourcorp/app/jdbc/Queries.properties" to this method.
	 * @throws IOException
	 *             if a file access error occurs
	 * @throws IllegalArgumentException
	 *             if the ClassLoader can't find a file at the given path.
	 * @return Map of query names to SQL values
	 */
	public synchronized Map<String, String> load(String path)
			throws IOException {
		Map<String, String> queryMap = (Map<String, String>) this.queries
				.get(path);
		if (queryMap == null) {
			queryMap = this.loadQueries(path);
			this.queries.put(path, queryMap);
		}
		return queryMap;
	}

	/**
	 * Loads a set of named queries into a Map object. This implementation reads
	 * a properties file at the given path.
	 * 
	 * @param path
	 *            The path that the ClassLoader will use to find the file.
	 * @throws IOException
	 *             if a file access error occurs
	 * @throws IllegalArgumentException
	 *             if the ClassLoader can't find a file at the given path.
	 * @since DbUtils 1.1
	 * @return Map of query names to SQL values
	 */
	@SuppressWarnings("unchecked")
	protected Map<String, String> loadQueries(String path) throws IOException {
		// Findbugs flags getClass().getResource as a bad practice; maybe we
		// should change the API?
		InputStream in = getClass().getResourceAsStream(path);
		if (in == null) {
			throw new IllegalArgumentException(path + " not found.");
		}
		Properties props = new Properties();
		props.load(in);
		// Copy to HashMap for better performance
		return new HashMap(props);
	}

	/**
	 * Removes the queries for the given path from the cache.
	 * 
	 * @param path
	 *            The path that the queries were loaded from.
	 */
	public synchronized void unload(String path) {
		this.queries.remove(path);
	}
}
